﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
using EHTWebsiteProject.Code.Entity;


namespace EHTWebsiteProject.Administrator
{
    public partial class AdminFAQsUI : System.Web.UI.Page
    {

        public Boolean IsVN = true;      
        private String languageCode = "vi-VN";       
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private Int32 ID = HangSo.DEFAULT_ID;       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserName"] == null)
                    Response.Redirect("../Account/EHTLogin.aspx");
                else
                {                  
                    try
                    {
                        languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                        if (languageCode == "en-us")
                        {
                            IsVN = false;
                        }

                    }
                    catch (Exception ex)
                    {
                    }
                    if (Request.QueryString["ID"] !=null)
                        ID = Int32.Parse(Request.QueryString["ID"].ToString());                  
                    else
                        ID = HangSo.DEFAULT_ID;

                    if (Request.QueryString["Message"] != null)
                        ltrSuccess.Text = HelpMethod.ApplySuccess(Request.QueryString["Message"].ToString());

                    Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                    LoadFAQs(ID);                                        
                }
            }
        }


        public void LoadFAQs(Int32 ID)
        {
            if (ID != -1)
            {
                ViewState["action"] = HangSo.ACTION_UPDATE;
                ViewState["ID"] = ID.ToString();
                if (IsVN == true)
                    lbAdd_Edit.Text = HangSo.Edit_VN;
                else
                    lbAdd_Edit.Text = HangSo.Edit_Other;
                FAQs theFAQs = FAQsProvider.GetFAQsbyID(ID);
                txtTitleFAQs.Text = theFAQs.TitleFAQs;
                RadEditor1.Content = theFAQs.ContentFAQs.ToString();
                ddlFAQs.SelectedValue = theFAQs.Type.ToString();

            }
            else
            {
                ViewState["action"] = HangSo.ACTION_ADD;
                if (IsVN == true)
                    lbAdd_Edit.Text = HangSo.Add_VN;
                else
                    lbAdd_Edit.Text = HangSo.Add_Other;
            }
        }

        protected void ImgSave_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();

            FAQs theFAQs = new FAQs();
            theFAQs.TitleFAQs = this.txtTitleFAQs.Text.Trim();
            theFAQs.ContentFAQs = RadEditor1.Content.Trim();
            theFAQs.Type = Int32.Parse(ddlFAQs.SelectedValue.ToString().Trim());

            if (action == HangSo.ACTION_ADD)
            {
                theFAQs.ID = -1;
                FAQsProvider.IUFAQs(theFAQs, true);
            }
            else
            {
                theFAQs.ID = Int32.Parse(ViewState["ID"].ToString());
                FAQsProvider.IUFAQs(theFAQs, false);
            }

            Response.Redirect("AdminFAQs.aspx");
        }

        protected void ImgApply_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            Int32 IDOut;
            FAQs theFAQs = new FAQs();
            theFAQs.TitleFAQs = this.txtTitleFAQs.Text.Trim();
            theFAQs.ContentFAQs = RadEditor1.Content.Trim();
            theFAQs.Type = Int32.Parse(ddlFAQs.SelectedValue.ToString().Trim());

            if (action == HangSo.ACTION_ADD)
            {
                theFAQs.ID = -1;
                IDOut = FAQsProvider.IUFAQs(theFAQs, true);
            }
            else
            {
                theFAQs.ID = Int32.Parse(ViewState["ID"].ToString());
                IDOut = FAQsProvider.IUFAQs(theFAQs, false);

            }          

            Response.Redirect("AdminFAQsUI.aspx?ID=" + IDOut.ToString() + "&&Message="+HangSo.App_VN);
        }
    }
}